import requests
from DataTest.JsonData.read_json import load_json
import numpy as np
from jsonpath import jsonpath
from DataTest.ANR.path import *


def doll(cost_type):
    """
    :param cost_type: shell,diamond
    :return: 抓取结果和日志
    """
    self_token_list, match_token_list = load_json(json_data_path)

    for token in self_token_list:

        header = {
            'Authorization': f'Bearer {token}',
            'Content-Type': 'application/json',
        }

        url = 'http://124.220.33.63:8700' + f'/doll/v2/cost/{cost_type}'

        res = requests.post(url=url, headers=header)
        # print(res.json())

        _code = res.status_code
        request_list = ['true', 'false']

        if _code == 200:

            doll_url = 'http://124.220.33.63:8700' + '/doll/v2/result'
            result = np.random.choice(request_list, p=[1, 0])

            if result == 'false':
                print(f'用户{token}抓取失败')

            else:
                request = {
                    'result': True
                }

                doll_res = requests.post(url=doll_url, json=request, headers=header)
                # print(doll_res.json())

                doll_type_list = jsonpath(doll_res.json(), '$.data.ResponseType')
                doll_type = doll_type_list[0]

                print(f'用户{token}抓取的内容为{doll_type}')
